package cn.mastercom.sssvr.util;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;

public class txtWindowTab extends JFrame{
	
	/**
	 * 
	 */
	JTextArea textArea_1 = new JTextArea();  //直接预览文本
	private JTabbedPane jTabbedpane = new JTabbedPane();// 存放选项卡的组件
	private String[] tabNames = { "选项1", "选项2" };
	JTable table;
	JScrollPane scrollPane = new JScrollPane();
	
	
	
	public txtWindowTab(Object[] columnNames,Object[][] rowData,final String[] linestr) {
		setTitle("\u9884\u89C8");
		setSize(900, 546);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();  
	    setLocation((dim.width - getWidth()) / 2, (dim.height - getHeight()) / 2);  
		getContentPane().setLayout(new BorderLayout(0, 0));
		final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		
		
		//直接预览标签
		JPanel direct = new JPanel();
		tabbedPane.addTab("直接预览", null, direct, null);
		direct.setLayout(new BorderLayout(0, 0));
		
		JScrollPane jp = new JScrollPane();
		direct.add(jp, BorderLayout.CENTER);
		
		
		jp.setViewportView(textArea_1);
		
		JPanel panel_close = new JPanel();
		direct.add(panel_close, BorderLayout.SOUTH);
		
		JButton btnNewButton = new JButton("\u5173\u95ED");
		btnNewButton.addActionListener(arg0 -> setVisible(false));
		panel_close.add(btnNewButton);
	
		
		
		
		
		//表格预览标签
		final JPanel indirect = new JPanel();
		indirect.setToolTipText("\u9884\u89C8");
		tabbedPane.addTab("表格预览", null, indirect, null);
		indirect.setLayout(new BorderLayout(0, 0));
		indirect.add(scrollPane, BorderLayout.CENTER);
		
	//使用模式建表
		DefaultTableModel model = new DefaultTableModel(rowData,columnNames);
		table = new JTable (model);
		TableColumn column = null;  
        int colunms = table.getColumnCount();  
        for(int i = 0; i < colunms; i++)  
        {  
            column = table.getColumnModel().getColumn(i);  
            /*将每一列的默认宽度设置为100*/  
            column.setPreferredWidth(100);  
        }  
        
        /* 
         * 设置JTable自动调整列表的状态，此处设置为关闭 
         */  
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  
        
		final TableRowSorter sorter = new TableRowSorter(model);
        table.setRowSorter(sorter); //为JTable设置排序器

		scrollPane.setViewportView(table);
		//退出按钮
		JPanel panel = new JPanel();
		indirect.add(panel, BorderLayout.SOUTH);
		
				panel.setLayout(null);
				panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
				JLabel lblNewLabel = new JLabel("\u9009\u62E9\u65B0\u7684\u5206\u5272\u7B26");
				lblNewLabel.setLocation(200, 9);
				lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
				panel.add(lblNewLabel);
				String str1[] = {",","\\t","_","\\|"};
				final JComboBox comboBox = new JComboBox<String>(str1);
				panel.add(comboBox);
				comboBox.setEditable(true);
				JButton btnNewButton_2 = new JButton("\u786E\u5B9A");
				btnNewButton_2.setVerticalAlignment(SwingConstants.BOTTOM);
				btnNewButton_2.setHorizontalAlignment(SwingConstants.RIGHT);
				panel.add(btnNewButton_2);
				btnNewButton_2.addActionListener(e -> {
					String separator = comboBox.getSelectedItem().toString();
					newsplit(separator, linestr);

				});
				JButton btnNewButton_1 = new JButton("\u9000\u51FA");
				panel.add(btnNewButton_1);
				btnNewButton_1.setHorizontalAlignment(SwingConstants.RIGHT);
				btnNewButton_1.addActionListener(arg0 -> setVisible(false));
		
		
		
		
		
		
		
		
		
		
		
		/*按新的分割符预览
		 * 传入新的分割符
		 */
	}
	

	//显示文件内容
	public void setShowFile(String str) {
		// TODO Auto-generated method stub
		
		textArea_1.setText(str);
		 
	}
	public void newsplit(String separator,String[] linestr ){
		//求分割后每行的长度
		 String[] result = linestr[0].split(separator);
		
		 
		//列名
		 final String[] columnNames = new String[result.length];
		 for(int i=0;i<result.length;i++){
			 columnNames[i]= "c" + i;
		 }
		
	//	String[] line = str.split("\n");
		 
		 //把字符串存储到二维数组中
		 
		 
			Object[][] rowData = new Object[linestr.length][result.length];
			for(int l = 0;l<linestr.length;l++){
				String[] newlinestr = linestr[l].split(separator);
				for(int r = 0;r<result.length;r++)
				//使用0填充空缺的值，使每行的值个数相同
				try{
					rowData[l][r] = newlinestr[r];}
				catch (Exception e) {
					rowData[l][r] = 0;
				}
			}
		DefaultTableModel model = new DefaultTableModel(rowData,columnNames);
		table = new JTable (model);
		TableColumn column = null;  
        int colunms = table.getColumnCount();  
        for(int i = 0; i < colunms; i++)  
        {
            column = table.getColumnModel().getColumn(i);  
            /*将每一列的默认宽度设置为100*/  
            column.setPreferredWidth(100);  
        }  
        
        /* 
         * 设置JTable自动调整列表的状态，此处设置为关闭 
         */  
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  
		final TableRowSorter sorter = new TableRowSorter(model);
        table.setRowSorter(sorter); //为JTable设置排序器
		scrollPane.setViewportView(table);
	}
}
